<template>
  <div style="background-color: #f6f6f6; min-height: 100vh;">
    <div class="front-notice"><i class="el-icon-bell" style="margin-right: 2px"></i>公告：{{ top }}</div>
    <!--头部-->
    <div class="front-header">
      <div class="front-header-left">
        <img src="@/assets/imgs/logo.png" alt="">
        <div class="title">社区养老系统</div>
      </div>
      <div class="front-header-center">
        <div class="front-header-nav">
          <el-menu 
            :default-active="$route.path" 
            mode="horizontal" 
            router
            :style="{ 
              height: elderMode ? '80px' : '60px',
              'font-size': elderMode ? '24px' : '16px'
            }"
            style="display: flex; 
                   justify-content: flex-start;  
                   align-items: center;
                   flex-wrap: nowrap;
                   padding-left: 10px;">
            <el-menu-item index="/front/home">
              首页
            </el-menu-item>
            <el-menu-item index="/front/activity">公益活动</el-menu-item>
            <el-menu-item index="/front/serve">养老服务</el-menu-item>
            <el-menu-item index="/front/nurseHouse">养老院</el-menu-item>
            <el-menu-item index="/front/person">个人中心</el-menu-item>
          </el-menu>
        </div>
      </div>
      <div class="front-header-right">
        <el-button type="text" @click="toggleElderMode" class="mode-toggle-btn" 
                  :style="{ color: elderMode ? '#FF6B35' : '#FF6B35' }">
                  <i :class="elderMode ? 'el-icon-close' : 'el-icon-user'"></i>
                  {{ elderMode ? '退出关怀模式' : '关怀模式' }}
                </el-button>
        <div class="auth-buttons" v-if="!user.username">
          <el-button class="login-btn" @click="$router.push('/login')">登录</el-button>
          <el-button class="register-btn" @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown>
            <div class="front-header-dropdown">
              <img :src="user.avatar" alt="">
              <div style="margin-left: 10px">
                <span>{{ user.name }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
              </div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div @click="$router.push('/front/activitySign')">我报名的活动</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click="$router.push('/front/reserve')">我预约的服务</div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click="logout">退出登录</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!--主体-->
    <div class="main-body">
      <router-view ref="child" @update:user="updateUser" />
    </div>

    <Footer />

  </div>

</template>

<script>

import Footer from "@/components/Footer"
export default {
  name: "FrontLayout",
  components:{Footer},

  data () {
    return {
      top: '',
      notice: [],
      user: JSON.parse(localStorage.getItem("xm-user") || '{}'),
      elderMode: localStorage.getItem('elderMode') === 'true' // 从本地存储读取模式状态
    }
  },

  mounted() {
    this.loadNotice()
    this.applyElderModeStyles() // 初始化时应用样式

  },
  methods: {
    async toggleElderMode() {
        try {
          await this.$confirm('切换关怀模式需要重新加载页面，是否继续？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          this.elderMode = !this.elderMode
          localStorage.setItem('elderMode', this.elderMode)
          location.reload()
        } catch (e) {
          // 用户取消操作
        }
    },
    applyElderModeStyles() {
      const html = document.documentElement
      if (this.elderMode) {
        html.style.fontSize = '30px'
        html.style.lineHeight = '1.5'
        html.style.color = '#333'
        // 添加强制样式更新
        document.querySelectorAll('*').forEach(el => {
          el.style.setProperty('font-size', '1.05em', 'important')
          el.style.setProperty('line-height', '1.5', 'important')
        })
      } else {
        html.style.fontSize = ''
        html.style.lineHeight = ''
        html.style.color = ''
        document.querySelectorAll('*').forEach(el => {
          el.style.removeProperty('font-size')
          el.style.removeProperty('line-height')
        })
      }
    },
    loadNotice() {
      this.$request.get('/notice/selectAll').then(res => {
        this.notice = res.data
        let i = 0
        if (this.notice && this.notice.length) {
          this.top = this.notice[0].content
          setInterval(() => {
            this.top = this.notice[i].content
            i++
            if (i === this.notice.length) {
              i = 0
            }
          }, 2500)
        }
      })
    },
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    // 退出登录
    logout() {
      localStorage.removeItem("xm-user");
      this.$router.push("/login");
    },
  }

}
</script>

<style scoped>
  @import "@/assets/css/front.css";
</style>
